<?php
namespace App\Utilities;

use App\Utilities\AlipayAop\AopClient;
use App\Utilities\AlipayAop\AlipayFundTransOrderQueryRequest;
use App\Utilities\AlipayAop\AlipayFundTransToaccountTransferRequest;
use App\Utilities\AlipayAop\AlipayTradeWapPayRequest;

class Alipay
{
    /**
     * 作用：支付宝对接
     * 制作者：九哥
     * 时间：2019-07-26
     */

    private $appId;
    private $gatewayUrl = 'https://openapi.alipay.com/gateway.do';
    private $rsaPrivateKey; //私钥
    private $alipayRsaPublicKey; //支付宝公钥
    private $aop;

    public function __construct()
    {
        $this->appId = '2019072565963731';
        $this->rsaPrivateKey = $this->getRsaPrivateKey();
        $this->alipayRsaPublicKey = $this->getAlipayRsaPublicKey();
    }

    /**
     * 充值
     * 下单接口
     * @param $order_number 订单号
     * @param $amount  转账金额
     * @param $quit_url  用户付款中途退出返回商户网站的地址
     * @param $return_url  同步通知地址
     * @param $notify_url  异步通知地址
     */
    public function makeOrder($order_number,$amount,$quit_url,$return_url,$notify_url)
    {
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = $this->appId;
        $aop->rsaPrivateKey = $this->getRsaPrivateKey();
        $aop->alipayrsaPublicKey = $this->getAlipayRsaPublicKey();
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $request = new AlipayTradeWapPayRequest();
        $bizcontent = json_encode(array(
            //商品的标题/交易标题/订单标题/订单关键字等
            'subject' => '美策客户充值',
            //商户网站唯一订单号
            'out_trade_no' => $order_number,
            //总金额
            'total_amount' => $amount,
            //用户付款中途退出返回商户网站的地址
            'quit_url' => $quit_url,
            'product_code' => 'QUICK_WAP_WAY',
        ));
        // 支付结果同步通知地址
        $request->setReturnUrl($return_url);
        // 支付结果异步通知地址
        $request->setNotifyUrl($notify_url);
        $request->setBizContent($bizcontent);
        $result = $aop->pageExecute($request);
        return $result;
//        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
//        $resultCode = $result->$responseNode->code;
//        if(!empty($resultCode)&&$resultCode == 10000){
//            echo true;
//        } else {
//            echo false;
//        }
    }

    /**
     * 提现
     * 单笔转账接口
     * @param $order_number 订单号
     * @param $pay_no  转账账号
     * @param $pay_name  转账用户名
     * @param $amount  转账金额
     * @param $remark   备注
     */
    public function makeOut($order_number,$pay_no,$pay_name,$amount,$remark)
    {
        $aop = new AopClient();
        $aop->gatewayUrl = $this->gatewayUrl;
        $aop->appId = $this->appId;
        $aop->rsaPrivateKey = $this->getRsaPrivateKey();
        $aop->alipayrsaPublicKey = $this->getAlipayRsaPublicKey();
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $request = new AlipayFundTransToaccountTransferRequest();
        $bizcontent = json_encode(array(
            //商户转账唯一订单号
            'out_biz_no' => $order_number,
            //商户网站唯一订单号
            'payee_type' => 'ALIPAY_LOGONID',
            //收款方账户
            'payee_account' => $pay_no,
            //转账金额单位元
            'amount' => $amount,
            //转账备注
            'remark' => $remark,
        ));
        $request->setBizContent($bizcontent);
        $result = $aop->execute($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;

      ///  var_dump($result->$responseNode->sub_msg);exit();
//        return $resultCode;
        if($resultCode && $resultCode == 10000){
            if($result->$responseNode->order_id){
                return $result->$responseNode->order_id;
            }
            return true;
        } else {
            $this->errorMsg=$result->$responseNode->sub_msg;
            return false;
        }
    }

    public $errorMsg='';

    public function getRsaPrivateKey()
    {
        return 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCalgVTpshLbdUZy5sY+U9jGPddaEaAEeOeUS1KKg8FbKZ3FXTPY9Se38MhJwsO9PaT76MYAANOV56l1vdD0ZeUsTlE+oDPiiWkFjoHrWDFUmNEV5pKA/fjFMx20gZvBMgC3ewz/tCiN2WGVJx+jTJ+G+nLu7v+T/1VWcfa//mvvY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